package com.cqut.hfh.ordering.service.Impl;

import com.cqut.hfh.ordering.pojo.*;
import com.cqut.hfh.ordering.service.IFodPicService;
import com.cqut.hfh.ordering.service.IMerchantService;
import com.cqut.li.mybatisutil.dao.base.Dao;
import com.cqut.li.mybatisutil.dao.base.SearchDao;
import com.cqut.li.mybatisutil.entity.base.Entity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * create by gtt on 2019/6/25
 */
@Service
public class MerchantServiceImpl implements IMerchantService {
    @Resource(name = "Dao")
    private Dao dao;
    @Resource
    private SearchDao searchDao;

    @Resource
    private IFodPicService fodPicService;

    @Override
    public List<Map<String, Object>> getMerchant(String limit, String page) {
        String con = " 1 = 1 limit "
                + (Integer.valueOf(page)-1)*Integer.valueOf(limit) + "," + limit ;
        String joinEntity = " left join merchant_type on fod_merchant.type_id = merchant_type.type_id";
        List<Map<String, Object>> merchant_list
                = searchDao.searchForeign(new String[]{"fod_merchant.merchant_id","merchant_type.type_name",
                        "fod_merchant.merchant_name",
                        "fod_merchant.merchant_phone",
                        "fod_merchant.merchant_site",
                        "fod_merchant.merchant_credit",
                        "fod_merchant.optime",
                        "fod_merchant.oper",
                        "fod_merchant.isva","fod_merchant.sort"},
                Entity.getTableName(FoodMerchant.class),
                joinEntity,
                null,
                null,
                con);

        return merchant_list;
    }
    @Override
    public Integer getCount() {
        return searchDao.sqlCount(Entity.getTableName(FoodMerchant.class));
    }

    @Override
    public List<Map<String, Object>> getMerchantsByName(String limit, String page, String merchant_name) {
        String con = " merchant_name like '%" + merchant_name + "%' limit "
                + (Integer.valueOf(page)-1)*Integer.valueOf(limit) + "," + limit ;
        String joinEntity = " left join fod_merchant on fod_merchant.type_id = merchant_type.type_id";
        List<Map<String, Object>> merchants_list
                = searchDao.searchForeign(new String[]{"fod_merchant.merchant_id","merchant_type.type_name",
                        "fod_merchant.merchant_name",
                        "fod_merchant.merchant_phone",
                        "fod_merchant.merchant_site",
                        "fod_merchant.merchant_credit",
                        "fod_merchant.optime",
                        "fod_merchant.oper",
                        "fod_merchant.isva","fod_merchant.sort"},
                Entity.getTableName(MerchantType.class),
                joinEntity,
                null,
                null,
                con);

        return merchants_list;
    }

    /**
     * 根据商家的id删除该商家
     * @param merchantId
     * @return
     */
    @Override
    public int delByMerchantId(String merchantId) {
        //删除商家
        int a = dao.deleteByID(merchantId, FoodMerchant.class);
        //删除商家图片
        String con=" bus_id= '"+ merchantId +"'";
        int b=dao.deleteByCondition(con,FoodPic.class);
        //删除商家对应的食品
        String con1=" merchant_id = '" + merchantId + "'";
        int c=dao.deleteByCondition(con1,FodFood.class);
        return a;
    }

    @Override
    public Map<String, Object> findById(String merchantId) {
        String con=" merchant_id = '" + merchantId + "'";
        //String joinEntity = " left join fod_pic on fod_merchant.merchant_id = fod_pic.bus_id";
        List<Map<String, Object>> foodMerchantVoList=searchDao.searchForeign(new String[]{"fod_merchant.merchant_id",
                        "fod_merchant.type_id",
                        "fod_merchant.merchant_name",
                        "fod_merchant.merchant_phone",
                        "fod_merchant.merchant_site",
                        "fod_merchant.merchant_credit",
                        "fod_merchant.optime",
                        "fod_merchant.oper",
                        "fod_merchant.isva","fod_merchant.sort"},
                Entity.getTableName(FoodMerchant.class),
                null,
                null,
                null,
                con);


        List<FoodPic> byCondition = dao.getByCondition(" bus_id='" + merchantId + "'", FoodPic.class);

        if ( byCondition != null && byCondition.size() != 0){
            foodMerchantVoList.get(0).put("pic_site", byCondition.get(0).getPic_site());
        }

        return foodMerchantVoList.get(0);
    }

    @Override
    public int updateMerchant(FoodMerchant merchant, String pic_site) {

        int a = dao.updatePropByID(merchant, merchant.getMerchant_id());
        //更新图片地址
        fodPicService.updatePic(merchant.getMerchant_id(), pic_site);

        return a;
    }

    @Override
    public int saveFoodMerchant(FoodMerchant foodMerchant, String pic_site) {
        int save = dao.save(foodMerchant);
        fodPicService.savePic(foodMerchant.getMerchant_id(), pic_site);
        return save;
    }

}
